package com.horay.springclouddemo.provider.hystrix.controller;

import com.horay.springclouddemo.common.http.Responses;
import com.horay.springclouddemo.common.http.ResultBody;
import com.horay.springclouddemo.model.hystrix.Hystrix;
import com.horay.springclouddemo.model.hystrix.HystrixDTO;
import com.horay.springclouddemo.provider.hystrix.service.HystrixService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 豪猪控制器
 *
 * @author heyingcheng
 * @date 2020/3/21 13:52
 */
@RestController
@RequestMapping("hystrix")
public class HystrixController {

    private final HystrixService hystrixService;

    public HystrixController(HystrixService hystrixService) {
        this.hystrixService = hystrixService;
    }

    @PostMapping
    public ResponseEntity<ResultBody> createHystrix(Hystrix hystrix) {
        if (hystrixService.createHystrix(hystrix)) {
            return Responses.ok();
        } else {
            return Responses.unprocessableEntity();
        }
    }

    @GetMapping("{id}")
    public ResponseEntity<ResultBody<Hystrix>> getHystrix(@PathVariable Integer id) {
        return Responses.ok(hystrixService.getHystrix(id));
    }

    @GetMapping("hystrixs")
    public ResponseEntity<ResultBody<List<Hystrix>>> getHystrixs(@RequestBody HystrixDTO hystrixDTO) {
        return Responses.ok(hystrixService.getHystrixs(hystrixDTO));
    }

    @PutMapping
    public ResponseEntity<ResultBody> updateHystrix(Hystrix hystrix) {
        if (hystrixService.updateHystrix(hystrix)) {
            return Responses.ok();
        } else {
            return Responses.unprocessableEntity();
        }
    }

    @DeleteMapping("{id}")
    public ResponseEntity<ResultBody> deleteHystrix(@PathVariable Integer id) {
        if (hystrixService.deleteHystrix(id)) {
            return Responses.ok();
        } else {
            return Responses.unprocessableEntity();
        }
    }

}
